package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "C端地址簿接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation("查询地址")
    public Result<List<AddressBook>> list(){
        return Result.success(addressBookService.findAddressBooks());
    }

    @PostMapping("")
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址:{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址");
        return Result.success(addressBookService.getDefault());
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址:{}",addressBook);
        addressBookService.updataStatus(addressBook);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("查询指定地址")
    public Result<AddressBook> get(@PathVariable Long id){
        log.info("查询指定地址:{}",id);
        return Result.success(addressBookService.getById(id));
    }

    @PutMapping("")
    @ApiOperation("修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}",addressBook);
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    @DeleteMapping("")
    @ApiOperation("删除地址")
    public Result delete(Long id){
        log.info("删除地址:{}",id);
        addressBookService.delete(id);
        return Result.success();
    }
}
